﻿using System;
using System.Configuration;
using System.Linq;
using System.Web.UI.WebControls;
using Controllers;

namespace Web
{
    public partial class AssetEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);

                IQueryable<Employee> employees = ctx.Employees.OrderBy(r => r.Name).Where(r => r.LeaveDate == null);
                Keeper.DataSource = employees;
                Keeper.DataTextField = "Name";
                Keeper.DataValueField = "Id";
                Keeper.DataBind();
                Keeper.Items.Insert(0, new ListItem("请选择", ""));

                string no = Request.QueryString["no"];
                if (!string.IsNullOrEmpty(no))
                {
                    Asset asset = ctx.Assets.Single(r => r.No == no);
                    No.Value = asset.No;
                    No.Disabled = true;

                    Name.Value = asset.Name;
                    Sn.Value = asset.Sn;
                    Class.Value = asset.Class;
                    Model.Value = asset.Model;
                    Spec.Value = asset.Spec;
                    Unit.Value = asset.Unit;
                    Supplier.Value = asset.Supplier;
                    Price.Value = asset.Price;
                    BuyDate.Value = asset.BuyDate != null ? ((DateTime)asset.BuyDate).ToString("yyyy-MM-dd") : "";
                    Status.Value = asset.Status;
                    DistributeDate.Value = asset.DistributeDate != null ? ((DateTime)asset.DistributeDate).ToString("yyyy-MM-dd") : "";
                    DueTime.Value =asset.DueTime!=null?asset.DueTime.ToString():"";
                    Keeper.Value = asset.KeeperId.ToString();
                    Remarks.Value = asset.Remarks;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            Asset asset;
            if (No.Disabled)
            {
                asset = ctx.Assets.Single(r => r.No ==No.Value);
            }
            else
            {
                asset = new Asset();
                asset.No = No.Value;
                ctx.Assets.InsertOnSubmit(asset);
            }
            asset.Name = Name.Value;
            asset.Sn = Sn.Value;
            asset.Class = Class.Value;
            asset.Model = Model.Value;
            asset.Spec = Spec.Value;
            asset.Unit = Unit.Value;
            asset.Supplier = Supplier.Value;
            asset.Price = Price.Value;
            if (string.IsNullOrEmpty(BuyDate.Value))
            {
                asset.BuyDate = null;
            }
            else
            {
                asset.BuyDate = DateTime.Parse(BuyDate.Value);
            }
            asset.Status = Status.Value;
            if (string.IsNullOrEmpty(DistributeDate.Value))
            {
                asset.DistributeDate = null;
            }
            else
            {
                asset.DistributeDate = DateTime.Parse(DistributeDate.Value);
            }
            if(string.IsNullOrEmpty(DueTime.Value))
            {
                asset.DueTime = null;
            }
            else
            {
                asset.DueTime =int.Parse(DueTime.Value);
            }
            asset.KeeperId = int.Parse(Keeper.Value);
            asset.Remarks = Remarks.Value;

            ctx.SubmitChanges();
            Response.Write("<script>window.returnValue='Succeed';window.close();</script>");
        }
    }
}